/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package panel;

import java.awt.FontMetrics;
import java.awt.Image;
import java.util.Base64;
import javax.swing.ImageIcon;

/**
 *
 * @author jerry
 */
public class MeMsgBoxPanel extends javax.swing.JPanel {
 /**
     * 要统一的高度
     */
    public int PanelHeight;
    /**
     * Creates new form MeMsgBoxPanel
     *
     * @param msg_textString 消息内容
     * @param use_face_iamge
     * @param msg_image 消息图片
     */
    public MeMsgBoxPanel(String msg_textString, Image use_face_iamge, String msg_image) {
        initComponents();
        if (!msg_image.equals("")) {
             byte[] use_image_byte = Base64.getDecoder().decode(msg_image);
            ImageIcon useimageicon = new ImageIcon(use_image_byte);
            PanelHeight = useimageicon.getIconHeight();
            msg_text.insertIcon(useimageicon);
            //改变大小
        } else {
            msg_text.setText("<html>" + msg_textString + "</html>");
            //通过计算字符的长宽来人工决定显示的界面的长宽
            FontMetrics fm = msg_text.getFontMetrics(msg_text.getFont());
            int string_width = fm.stringWidth(msg_text.getText());//获得字符的宽度
            int string_hight = fm.getHeight();//获得高度
            int string_leading = fm.getLeading();//获得字符的间距
            //最后 高=宽+间距,长度=总长度除以固定的面板长度, 面板最终使用的 高=总长度除以的数 乘以 字高
            //以上获得了字符串的长和宽,宽实际就是一行的大小,用除法
            PanelHeight = (string_width / 250) * (string_hight);
        }

        face_image.setIcon(new ImageIcon(use_face_iamge));

    }

    /**
     * 获得文本框的大小
     *
     * @return
     */
    public int GetMsgSize() {
       return PanelHeight;
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        face_image = new javax.swing.JLabel();
        msg_text = new javax.swing.JTextPane();

        setPreferredSize(new java.awt.Dimension(567, 0));

        msg_text.setBackground(new java.awt.Color(178, 226, 129));
        msg_text.setBorder(new javax.swing.border.LineBorder(new java.awt.Color(178, 226, 129), 5, true));
        msg_text.setContentType("text/html"); // NOI18N
        msg_text.setSelectionColor(new java.awt.Color(0, 0, 0));

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
        this.setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addContainerGap(219, Short.MAX_VALUE)
                .addComponent(msg_text, javax.swing.GroupLayout.PREFERRED_SIZE, 290, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addComponent(face_image, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(msg_text)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(face_image, javax.swing.GroupLayout.PREFERRED_SIZE, 40, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
        );
    }// </editor-fold>//GEN-END:initComponents


    // Variables declaration - do not modify//GEN-BEGIN:variables
    public javax.swing.JLabel face_image;
    private javax.swing.JTextPane msg_text;
    // End of variables declaration//GEN-END:variables
}
